<?php
function D($model_name){
    $model_name = $model_name.'Model';
    require_once 'application/model/'.$model_name.'.php';
    $ob = new $model_name();
    return $ob;
}
function M(){
    require_once 'application/core/BaseModel.php';
    $ob = new BaseModel();
    return $ob;
}
class BaseController
{   
    private $smarty = null;
    public function __construct(){
        include_once 'library/smarty/Smarty.class.php';
        $smarty = new Smarty();
        $this->smarty = $smarty;
        $smarty->setTemplateDir('./application/view/');
        $smarty->setCompileDir('./application/runtime/templates_c/');
        $smarty->setCacheDir('./application/runtime/cache/');
        $smarty->setConfigDir('./application/configs/');
        $uid = cookie('uid');
        $smarty->assign('ROOT',ROOT);
        $smarty->assign('APP',APP);
        if(!empty($uid)){
            $userInfo = D('user')->getUserInfoById(decode($uid));
            $smarty->assign('userInfo',$userInfo);
        }
    }
    protected function checkLoginStatus(){
        $uid = cookie('uid');
        if(empty($uid)){
            $this->redirect(APP.'/user/login.html');
        }
    }
    protected function display($tpl){
        $this->smarty->display($tpl);
    }
    protected function assign($key,$value){
        $this->smarty->assign($key,$value);
    }
    protected function redirect($url,$msg=null){
        echo '<script type="text/javascript">';
        if(!empty($msg)){
            echo 'alert("'.$msg.'");';
        }
        echo 'window.location.href="'.$url.'";';
        echo '</script>';
        exit();
    }
    protected function success($url,$msg){
        $_SESSION['url'] = $url;
        $_SESSION['msg'] = $msg;
        $this->redirect(APP.'/success/index.html');
    }
}